package kd.comt.plugin;

import java.util.EventObject;

import kd.bos.dataentity.utils.StringUtils;
import kd.bos.entity.property.LargeTextProp;
import kd.bos.form.control.Control;
import kd.bos.form.control.ImageList;
import kd.bos.form.control.events.UploadEvent;
import kd.bos.form.control.events.UploadListener;
import kd.bos.form.plugin.AbstractFormPlugin;
//区分



public class Album extends AbstractFormPlugin implements UploadListener {
    // 图片列表标识
    private String KEY_IMAGELIST1 = "bpgx_imagelistap";
    // 大文本标识
    private String LARGETEXTID = "bpgx_xcdwb_tag";

    // 1.注册监听
    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        // 侦听图片列表控件的点击、文件上传事件
        ImageList imageList = this.getView().getControl(KEY_IMAGELIST1);
        imageList.addUploadListener(this);
    }

    @Override
    public void afterCreateNewData(EventObject e) {
        // TODO Auto-generated method stub
        super.afterCreateNewData(e);
        // LargeTextEdit lt = this.getView().getControl("customerimagelisturl");
        // LargeTextField ltf = new LargeTextField();
        // ltf.setId(lt.getModel().getDataEntity().getString("id"));
        // ltf.setMaxLength(0);

    }

    @Override
    public void afterBindData(EventObject e) {
        super.afterBindData(e);
        // 设置图片列表默认显示的图片文件
        // 技巧: 可以在界面上添加一个图片控件，打开其图片属性，获取预置的各种图片文件URL地址
        String urls = (String) this.getModel().getValue(LARGETEXTID);
        ImageList imageList = this.getView().getControl(KEY_IMAGELIST1);
        if (urls != "") {
            String[] imageUrls = urls.toString().split(" ");
            imageList.setImageUrls(imageUrls);
        }
    }

    /**
     * // 上传完成后触发
     * 文件上传时触发；可以在此事件，获取已上传到文件服务器的文件URL
     */
    @Override
    public void upload(UploadEvent evt) {
        String fileUrls = this.parseFileUrls(evt);
        Control source = (Control) evt.getSource();
        if (StringUtils.equals(source.getKey(), KEY_IMAGELIST1)) {
            // 获取大文本字段值
            String reurl = (String) this.getModel().getValue(LARGETEXTID);
            if (!fileUrls.equals(""))
                reurl = reurl + fileUrls;
            else {
                reurl = reurl.toString();
            }
            // 将url赋值给大文本
            this.getModel().setValue(LARGETEXTID, reurl);
        }
        LargeTextProp tp = (LargeTextProp) this.getModel().getDataEntityType().getProperty(LARGETEXTID);
        int index = tp.getMaxLenth();
        System.out.println(tp);
    }

    /**
     * // 删除完成后触发
     */
    @Override
    public void remove(UploadEvent evt) {
        // TODO Auto-generated method stub
        UploadListener.super.remove(evt);
        // 删除上传的图片
        Control source = (Control) evt.getSource();
        if (StringUtils.equals(source.getKey(), KEY_IMAGELIST1)) {
            // 获取删除图片列表的下标值
            Object[] dele = evt.getIndexes();
            Integer delenumber = (Integer) dele[0];
            // 获取大文本字段值
            String fileUrls = (String) this.getModel().getValue(LARGETEXTID);
            String[] reurllist = fileUrls.split(" ");

            for (int i = delenumber; i < reurllist.length - delenumber - 1; i++) {
                reurllist[i] = reurllist[i + 1];
            }
            reurllist[reurllist.length - 1] = "";
            StringBuffer reurl = new StringBuffer();
            // 处理删除这种图片url后的url
            for (int i = 0; i < reurllist.length - 1; i++) {
                reurl.append(reurllist[i]);
                reurl.append(" ");
            }
            this.getModel().setValue(LARGETEXTID, reurl);
        }
    }

    private String parseFileUrls(UploadEvent evt) {
        // 从事件的传入参数中，读取已上传到文件服务器的文件地址
        Object[] urls = evt.getUrls();
        if (urls.equals(null)) {
            return null;
        }
        StringBuffer urlsbuf = new StringBuffer();
        for (Object url : urls) {
            urlsbuf.append(url);
            urlsbuf.append(" ");
        }
        return urlsbuf.toString();
    }
}